<?php

namespace Common\Lib\Payments\Yeepay;

use \Common\Lib\Payments\PaymentPlugin;

/**
 * @class Yeepay
 * @brief 易宝支付插件类
 */
class YeepayPC extends PaymentPlugin {

    /**
     * @see paymentplugin::getSubmitUrl()
     */
    public function getSubmitUrl() {
        return 'https://www.yeepay.com/app-merchant-proxy/node';
    }

    /**
     * @see paymentplugin::notifyStop()
     */
    public function notifyStop() {
        echo "success";
    }

    public function isReturn($callbackData) {
        return intval($callbackData['r9_BType']) === 1;
    }

    /**
     * @see paymentplugin::callback()
     */
    public function callback($callbackData) {
        $reply = $this->reply();
        $callbackData['p1_MerId'] = $this->config('M_PartnerId');
        if (!$this->checkHmac($callbackData, $this->config('M_PartnerKey'))) {
            $reply->message = '签名不正确';
            return $reply;
        }

        //回传数据
        $reply->orderNo = $callbackData['r6_Order'];
        $reply->tradeNo = $callbackData['r2_TrxId'];
        $reply->money = $callbackData['r3_Amt'];
        $reply->setStatus($callbackData['r1_Code'] == 1);

        return $reply;
    }

    /**
     * @see paymentplugin::notifyCallback()
     */
    public function notifyCallback($callbackData) {
        return $this->callback($callbackData);
    }

    /**
     * @see paymentplugin::getSendData()
     */
    public function getSendData($payment) {
        $return = array();
        //基本参数
        $return['p0_Cmd'] = 'Buy';
        $return['p1_MerId'] = $payment['M_PartnerId'];
        $return['p2_Order'] = $payment['M_OrderNO'];
        $return['p3_Amt'] = $payment['M_Amount'];
        $return['p4_Cur'] = 'CNY';
        $return['p5_Pid'] = $payment['M_OrderNO']; //商品名称
        $return['p6_Pcat'] = ''; //商品种类
        $return['p7_Pdesc'] = ''; //商品描述
        $return['p8_Url'] = $this->callbackUrl;

        $return['p9_SAF'] = 0; //送货地址
        $return['pa_MP'] = ''; //商户扩展信息
        $return['pd_FrpId'] = ''; //支付通道编码

        $return['hmac'] = $this->hmac(implode('', $return), $payment['M_PartnerKey']);
		
        return $return;
    }

    protected function checkHmac($data, $key) {
        $hmac = $data['hmac'];
        $keys = array('p1_MerId', 'r0_Cmd', 'r1_Code', 'r2_TrxId', 'r3_Amt', 'r4_Cur', 'r5_Pid', 'r6_Order', 'r7_Uid', 'r8_MP', 'r9_BType');
        $data = array_intersect_key($data, array_flip($keys));

        return $this->hmac(implode('', $data), $key) === $hmac;
    }

    protected function hmac($data, $key) {
        // RFC 2104 HMAC implementation for php.
        // Creates an md5 HMAC.
        // Eliminates the need to install mhash to compute a HMAC
        // Hacked by Lance Rushing(NOTE: Hacked means written)
        //需要配置环境支持iconv，否则中文参数不能正常处理
        $key = iconv("GB2312", "UTF-8", $key);
        $data = iconv("GB2312", "UTF-8", $data);

        $b = 64; // byte length for md5
        if (strlen($key) > $b) {
            $key = pack("H*", md5($key));
        }
        $key = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));
        $k_ipad = $key ^ $ipad;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack("H*", md5($k_ipad . $data)));
    }

}